package pCloud.client;

import java.net.InetSocketAddress;

import org.apache.log4j.FileAppender;
import org.apache.log4j.Logger;
import org.apache.log4j.SimpleLayout;

import pCloud.config.PCloudConfig;
import pCloud.config.PCloudDirectRequestConfig;
import pCloud.message.cloud.DataMessage;
import pCloud.message.cloud.HandShakeMessage;
import pCloud.transport.Communicator;
import pCloud.transport.SocketCommunicator;
import pCloud.transport.TransportHandle;

public class DataUser extends DataOwnerSimple{
	public DataUser(String configFile, int id) throws Exception {
		super(configFile, id);		
	}
	
	void init() throws Exception {
		PCloudConfig.init(this.configFile);
		
		logger = initLog("User" + clientId);
		communicator.init(logger);

		String[] server = PCloudConfig.getString(
				PCloudDirectRequestConfig.SERVER).split(" ");
		InetSocketAddress serverAdd = new InetSocketAddress(server[0],
				new Integer(server[1]).intValue());

		connector = new DataUserCloudConnector(serverAdd.getHostName(),
				serverAdd.getPort(), this.communicator);

		cloudAddress = serverAdd;		
	}
		
	//send a HandShakeMessage, then listen for DataMessage 
	public void doIt() throws Exception{
		//1. HandShakeMessage
		HandShakeMessage hsm = new HandShakeMessage(false,this.clientId); 
		TransportHandle conn = this.communicator.getConnection(this.cloudAddress);		
		conn.send(hsm);
		//this.communicator.send(this.cloudAddress, hsm);		
				
		DataUserCryptoHandle duch = new DataUserCryptoHandle();
		//2. listen to the DataMessage
	}
	
	public static void main(String[] args) throws Exception{
		DataUser du = new DataUser(args[0], new Integer(args[1]).intValue());
		du.doIt();
	}
}
